var restoreIpAddresses = function(s) {
  if (s.length > 12) return [];

  const ans = [], path = [];

  backtracking(0, s, ans, path);

  return ans;

};

function backtracking(startIndex, s, ans, path) {
  if (path.length === 4 && startIndex >= s.length) {
    ans.push([...path].join('.'));
    return;
  }

  for (let i = startIndex; i < s.length; i++) {
    if(path.length > 4) return;

    let subStr = s.substring(startIndex, i + 1 <= s.length ? i + 1 : s.length);
    if (isOk(subStr)) {
      path.push(subStr);
      backtracking(i + 1, s, ans, path);
      path.pop();
    } else {
      continue;
    }
  }

}

function isOk(str) {
  // 先判断前导0的个数
  if (str.length <= 1) return true;
  if (str[0] === '0') return false;
  if (Number(str) > 255) return false;
  return true;
} 

const s = "101023";
console.log(restoreIpAddresses(s))